﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ContourBuilder.Components.Dimensions;
using System.Drawing;

using ITAP.ContourBuilder.Components;
using System.Windows.Forms;
using ITAP.Core.Modelling;

namespace ITAP.ContourBuilder.Manipulation
{
    public class CommandAddArcRadiusDimension : SerializedCommand
    {
        public CommandAddArcRadiusDimension(ContourControl control)
            : base(control)
        {
        }

        ArcRadiusDimension dimension = null;
        PointF prevMousePoint;
        bool isMoved = false;
        public override void ObtainPeripheryState(GlobalPeripheryState state)
        {
            base.ObtainPeripheryState(state);
            // Выбираем элемент для создания размера
            if (state.MouseState.MouseMode == MouseMode.Down && state.MouseState.State.Button == MouseButtons.Left)
            {
                IEnumerable<IContourComponent> allComponents = this.control.ContourContainer.GetAllInnerComponents();
                ModelPoint point = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                ContourElement element = null;
                foreach (IContourComponent component in allComponents)
                    if (component is ContourElement && component.CanSelectByPoint(point))
                        element = component as ContourElement;

                if (element == null || element.Height == 0)
                {
                    this.Cancel();
                    return;
                }

                dimension = (ArcRadiusDimension)DimensionFactory.CreateArcRadiusDimension(element, GlobalData.DimensionTextFont, "R");
                element.GetRootModel().AddInnerComponent(dimension);
                this.control.ContourContainer.Update();
                prevMousePoint = state.MouseState.State.Location;
            }
            else if (state.MouseState.MouseMode == MouseMode.Move && state.MouseState.State.Button == MouseButtons.Left
                & dimension != null)
            {
                ModelPoint prevPoint = this.ScreenPointToModelPoint(prevMousePoint);
                ModelPoint point = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                ModelVector vector = new ModelVector(prevPoint, point);

                if (vector.Y != 0)
                    this.isMoved = true;

                dimension.OffsetFromPrimitive += vector.Y;

                prevMousePoint = state.MouseState.State.Location;
                dimension.Recalculate();
                this.control.Invalidate();
            }
            else if (state.MouseState.MouseMode == MouseMode.Up && state.MouseState.State.Button == MouseButtons.Left)
            {
                if (isMoved)
                {
                    this.control.UpdateModel();
                    this.Exit();
                }
                else
                {
                    dimension.Parent.RemoveInnerComponent(dimension);
                    this.control.UpdateModel();
                    this.Cancel();
                }
            }
        }

        public override string ToString()
        {
            return "Создать размер радиуса 1";
        }
    }
}
